/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.ui;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import de.tor.tribes.reportserver.IShareListener;
import de.tor.tribes.reportserver.MyVaadinApplication;
import de.tor.tribes.reportserver.util.UIUtil;
import de.tor.tribes.types.ZebraReport;
import de.tor.tribes.types.ZebraGroupMembership;
import de.tor.tribes.types.ZebraReportAccessGrant;
import de.tor.tribes.types.ZebraUser;
import de.tor.tribes.types.ZebraGroup;
import java.util.Collection;

/**
 *
 * @author Torridity
 */
public class ShareScreen extends CustomComponent {

    private GridLayout mainLayout;
    private TextField shareReceiver;
    private Button shareButton;
    private Button cancelShareButton;
    private CheckBox shareAll;
    private CheckBox shareSource;
    private CheckBox shareAttackers;
    private CheckBox shareLostAttackers;
    private CheckBox shareTarget;
    private CheckBox shareDefenders;
    private CheckBox shareLostDefenders;
    private CheckBox shareHaul;
    private CheckBox shareSpyInfo;
    private Collection<ZebraReport> theReports;
    private IShareListener listener;

    public ShareScreen(Collection<ZebraReport> theReports, IShareListener pListener) {
        buildMainLayout();
        this.theReports = theReports;
        listener = pListener;
    }

    private void buildMainLayout() {
        setWidth("300px");
        shareReceiver = new TextField("Freigeben für");
        shareReceiver.setNullSettingAllowed(false);
        shareReceiver.setNullRepresentation("");
        shareReceiver.setInputPrompt("Benutzer/Gruppe");
        shareReceiver.setRequired(true);

        shareAll = new CheckBox("Alles anzeigen");
        shareAll.setImmediate(true);
        shareAll.setValue(true);
        shareAll.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                shareSource.setValue(shareAll.getValue());
                shareAttackers.setValue(shareAll.getValue());
                shareLostAttackers.setValue(shareAll.getValue());
                shareTarget.setValue(shareAll.getValue());
                shareDefenders.setValue(shareAll.getValue());
                shareLostDefenders.setValue(shareAll.getValue());
                shareHaul.setValue(shareAll.getValue());
                shareSpyInfo.setValue(shareAll.getValue());
            }
        });
        Button.ClickListener deselectAll = new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                shareAll.setValue(Boolean.FALSE);
            }
        };
        shareSource = new CheckBox("Herkunft anzeigen");
        shareSource.addListener(deselectAll);
        shareSource.setImmediate(true);
        shareSource.setValue(true);
        shareAttackers = new CheckBox("Angreifer anzeigen");
        shareAttackers.addListener(deselectAll);
        shareAttackers.setImmediate(true);
        shareAttackers.setValue(true);
        shareLostAttackers = new CheckBox("Verluste anzeigen");
        shareLostAttackers.addListener(deselectAll);
        shareLostAttackers.setImmediate(true);
        shareLostAttackers.setValue(true);

        shareTarget = new CheckBox("Ziel anzeigen");
        shareTarget.addListener(deselectAll);
        shareTarget.setImmediate(true);
        shareTarget.setValue(true);
        shareDefenders = new CheckBox("Verteidiger anzeigen");
        shareDefenders.addListener(deselectAll);
        shareDefenders.setImmediate(true);
        shareDefenders.setValue(true);
        shareLostDefenders = new CheckBox("Verluste anzeigen");
        shareLostDefenders.addListener(deselectAll);
        shareLostDefenders.setImmediate(true);
        shareLostDefenders.setValue(true);

        shareHaul = new CheckBox("Beute anzeigen");
        shareHaul.addListener(deselectAll);
        shareHaul.setImmediate(true);
        shareHaul.setValue(true);
        shareSpyInfo = new CheckBox("Gebäude anzeigen");
        shareSpyInfo.addListener(deselectAll);
        shareSpyInfo.setImmediate(true);
        shareSpyInfo.setValue(true);

        shareButton = new Button("Freigeben", this, "shareReport");
        cancelShareButton = new Button("Abbrechen", this, "cancelShare");

        mainLayout = new UIUtil.GridLayoutBuilder(2, 11).
                addComponent(shareReceiver, 0, 0, 2, 1).
                addComponent(shareAll, 0, 1, 2, 1).
                addComponent(shareSource, 0, 2, 2, 1).
                addComponent(shareAttackers, 0, 3, 2, 1).
                addComponent(shareLostAttackers, 0, 4, 2, 1).
                addComponent(shareTarget, 0, 5, 2, 1).
                addComponent(shareDefenders, 0, 6, 2, 1).
                addComponent(shareLostDefenders, 0, 7, 2, 1).
                addComponent(shareHaul, 0, 8, 2, 1).
                addComponent(shareSpyInfo, 0, 9, 2, 1).
                addComponent(shareButton, 0, 10, 1, 1).
                addComponent(cancelShareButton, 1, 10, 1, 1).getLayout();
        mainLayout.setSpacing(true);
        setCompositionRoot(mainLayout);
    }

    public void shareReport() {
        if (!shareReceiver.isValid()) {
            return;
        }

        ZebraGroup group = ((MyVaadinApplication)getApplication()).getPersistence().getUserGroup((String) shareReceiver.getValue());
        if (group == null) {
            ZebraUser user = ((MyVaadinApplication)getApplication()).getPersistence().findUserByName((String) shareReceiver.getValue());
            if (user == null) {
                getWindow().showNotification("Warnung", "Es existiert keine Gruppe und kein Benutzer mit dem Namen " + (String) shareReceiver.getValue(), Window.Notification.TYPE_WARNING_MESSAGE);
            } else {
                shareReport(user);
            }
        } else {
            for (ZebraGroupMembership member : group.getMemberships()) {
                shareReport(member.getReportUser());
            }
        }
        listener.fireSharedEvent();
    }

    private void shareReport(ZebraUser pUser) {
        for (ZebraReport theReport : theReports) {
           // if (!theReport.getOwner().equals(pUser)) {
                ZebraReportAccessGrant share = new ZebraReportAccessGrant();
                share.setReport(theReport);
                share.setUser(pUser);
                share.setShareSource(shareSource.booleanValue());
                share.setShareAttackers(shareAttackers.booleanValue());
                share.setShareAttackerLosses(shareLostAttackers.booleanValue());
                share.setShareTarget(shareTarget.booleanValue());
                share.setShareDefenders(shareDefenders.booleanValue());
                share.setShareDefenderLosses(shareLostDefenders.booleanValue());
                share.setShareHaul(shareHaul.booleanValue());
                share.setShareSpyInfo(shareSpyInfo.booleanValue());
                ((MyVaadinApplication)getApplication()).getPersistence().persistReportShare(share);
           // }
        }
    }

    public void cancelShare() {
        listener.fireShareCanceledEvent();
    }
}
